from django.db import models

from assets.models import Asset

class World(models.Model):
  class Meta:
    db_table = 'Worlds'
    verbose_name_plural = "Worlds"

  name = models.CharField(max_length=256)

  def __unicode__(self):
    return self.name

OBJECT_STATE_CHOICES = (
    ('A', 'Added'),
    ('U', 'Updated'),
    ('D', 'Deleted'),
)   
    
class Object(models.Model):
  class Meta:
    db_table = 'Objects'
    verbose_name_plural = "Objects"

  name = models.CharField(max_length=256)
  world = models.ForeignKey(World)
  asset = models.ForeignKey(Asset)
  x = models.FloatField()
  y = models.FloatField()
  z = models.FloatField()
  
  m11 = models.FloatField(default=1.0)
  m12 = models.FloatField(default=0.0)
  m13 = models.FloatField(default=0.0)
  m21 = models.FloatField(default=0.0)
  m22 = models.FloatField(default=1.0)
  m23 = models.FloatField(default=0.0)
  m31 = models.FloatField(default=0.0)
  m32 = models.FloatField(default=0.0)
  m33 = models.FloatField(default=1.0)
  
  stamp = models.DateTimeField(auto_now=True, auto_now_add=True)
  
  state = models.CharField(max_length=1, choices=OBJECT_STATE_CHOICES, default='A')

  def __unicode__(self):
    return "%s(%d/%d) in world %s (%s)[%s]"%(self.name, self.id, self.asset.id, self.world.name, str(self.stamp), self.state)


TERRAIN_MODIFIER_TYPES = (
    ('R', 'Rectangle Flatten'),
    ('S', 'Sphere'),
    ('B', 'Brush'),
)     
    
class TerrainModifier(models.Model):
  class Meta:
    db_table = 'TerrainModifiers'
    verbose_name_plural = "TerrainModifiers"

  world = models.ForeignKey(World)
  type = models.CharField(max_length=1, choices=TERRAIN_MODIFIER_TYPES, default='R')
  typeData = models.CharField(max_length=256)
  
  x = models.FloatField()
  y = models.FloatField()
  z = models.FloatField()
  rad = models.FloatField(default=1.0)
  
  stamp = models.DateTimeField(auto_now=True, auto_now_add=True)
  
  state = models.CharField(max_length=1, choices=OBJECT_STATE_CHOICES, default='A')

  def __unicode__(self):
    return "%d(%s) in world %s"%(self.id, self.type, self.world.name)
